// 文件类型识别和图标映射
export interface FileTypeInfo {
  icon: string;
  description: string;
  category:
    | "document"
    | "image"
    | "video"
    | "audio"
    | "archive"
    | "executable"
    | "code"
    | "other";
}

export const FILE_TYPES: {
  [extension: string]: FileTypeInfo;
} = {
  // 文档
  txt: {
    icon: "Document",
    description: "文本文档",
    category: "document",
  },
  doc: {
    icon: "DocumentCopy",
    description: "Word 文档",
    category: "document",
  },
  docx: {
    icon: "DocumentCopy",
    description: "Word 文档",
    category: "document",
  },
  pdf: {
    icon: "Document",
    description: "PDF 文档",
    category: "document",
  },
  xls: {
    icon: "DocumentCopy",
    description: "Excel 表格",
    category: "document",
  },
  xlsx: {
    icon: "DocumentCopy",
    description: "Excel 表格",
    category: "document",
  },
  ppt: {
    icon: "DocumentCopy",
    description: "PowerPoint 演示文稿",
    category: "document",
  },
  pptx: {
    icon: "DocumentCopy",
    description: "PowerPoint 演示文稿",
    category: "document",
  },

  // 图片
  jpg: {
    icon: "Picture",
    description: "JPEG 图像",
    category: "image",
  },
  jpeg: {
    icon: "Picture",
    description: "JPEG 图像",
    category: "image",
  },
  png: {
    icon: "Picture",
    description: "PNG 图像",
    category: "image",
  },
  gif: {
    icon: "Picture",
    description: "GIF 图像",
    category: "image",
  },
  bmp: {
    icon: "Picture",
    description: "BMP 图像",
    category: "image",
  },
  svg: {
    icon: "Picture",
    description: "SVG 矢量图",
    category: "image",
  },
  webp: {
    icon: "Picture",
    description: "WebP 图像",
    category: "image",
  },

  // 视频
  mp4: {
    icon: "VideoCamera",
    description: "MP4 视频",
    category: "video",
  },
  avi: {
    icon: "VideoCamera",
    description: "AVI 视频",
    category: "video",
  },
  mov: {
    icon: "VideoCamera",
    description: "QuickTime 视频",
    category: "video",
  },
  wmv: {
    icon: "VideoCamera",
    description: "Windows Media 视频",
    category: "video",
  },
  flv: {
    icon: "VideoCamera",
    description: "Flash 视频",
    category: "video",
  },
  mkv: {
    icon: "VideoCamera",
    description: "Matroska 视频",
    category: "video",
  },

  // 音频
  mp3: {
    icon: "Headphones",
    description: "MP3 音频",
    category: "audio",
  },
  wav: {
    icon: "Headphones",
    description: "WAV 音频",
    category: "audio",
  },
  flac: {
    icon: "Headphones",
    description: "FLAC 无损音频",
    category: "audio",
  },
  aac: {
    icon: "Headphones",
    description: "AAC 音频",
    category: "audio",
  },
  ogg: {
    icon: "Headphones",
    description: "OGG 音频",
    category: "audio",
  },

  // 压缩文件
  zip: {
    icon: "Files",
    description: "ZIP 压缩文件",
    category: "archive",
  },
  rar: {
    icon: "Files",
    description: "RAR 压缩文件",
    category: "archive",
  },
  "7z": {
    icon: "Files",
    description: "7-Zip 压缩文件",
    category: "archive",
  },
  tar: {
    icon: "Files",
    description: "TAR 归档文件",
    category: "archive",
  },
  gz: {
    icon: "Files",
    description: "GZIP 压缩文件",
    category: "archive",
  },

  // 可执行文件
  exe: {
    icon: "Monitor",
    description: "Windows 可执行文件",
    category: "executable",
  },
  msi: {
    icon: "Monitor",
    description: "Windows 安装程序",
    category: "executable",
  },
  bat: {
    icon: "Monitor",
    description: "批处理文件",
    category: "executable",
  },
  cmd: {
    icon: "Monitor",
    description: "命令脚本",
    category: "executable",
  },

  // 代码文件
  js: {
    icon: "Document",
    description: "JavaScript 文件",
    category: "code",
  },
  ts: {
    icon: "Document",
    description: "TypeScript 文件",
    category: "code",
  },
  html: {
    icon: "Document",
    description: "HTML 文件",
    category: "code",
  },
  css: {
    icon: "Document",
    description: "CSS 样式表",
    category: "code",
  },
  vue: {
    icon: "Document",
    description: "Vue 组件",
    category: "code",
  },
  py: {
    icon: "Document",
    description: "Python 文件",
    category: "code",
  },
  java: {
    icon: "Document",
    description: "Java 文件",
    category: "code",
  },
  cpp: {
    icon: "Document",
    description: "C++ 文件",
    category: "code",
  },
  c: {
    icon: "Document",
    description: "C 文件",
    category: "code",
  },
  json: {
    icon: "Document",
    description: "JSON 数据",
    category: "code",
  },
  xml: {
    icon: "Document",
    description: "XML 文件",
    category: "code",
  },
};

// 获取文件类型信息
export function getFileTypeInfo(extension: string): FileTypeInfo {
  const ext = extension.toLowerCase();
  return (
    FILE_TYPES[ext] || {
      icon: "Document",
      description: "未知文件类型",
      category: "other",
    }
  );
}

// 格式化文件大小
export function formatFileSize(bytes: number): string {
  if (bytes === 0) return "0 B";

  const k = 1024;
  const sizes = ["B", "KB", "MB", "GB", "TB"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));

  return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
}

// 格式化日期
export function formatDate(date: string | Date): string {
  const d = typeof date === "string" ? new Date(date) : date;
  return d.toLocaleDateString("zh-CN", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
  });
}

// 判断是否为图片文件
export function isImageFile(extension: string): boolean {
  const imageExts = ["jpg", "jpeg", "png", "gif", "bmp", "svg", "webp"];
  return imageExts.includes(extension.toLowerCase());
}

// 判断是否为视频文件
export function isVideoFile(extension: string): boolean {
  const videoExts = ["mp4", "avi", "mov", "wmv", "flv", "mkv"];
  return videoExts.includes(extension.toLowerCase());
}

// 判断是否为音频文件
export function isAudioFile(extension: string): boolean {
  const audioExts = ["mp3", "wav", "flac", "aac", "ogg"];
  return audioExts.includes(extension.toLowerCase());
}
